/**
 * 
 */
package com.base.myproject.client.busi.b2c.analysis;

import java.util.Date;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.model.BarDataProvider;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.ScaleProvider;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.charts.BarChart;
import com.extjs.gxt.charts.client.model.charts.BarChart.BarStyle;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.i18n.client.DateTimeFormat;

/**
 * 单个商品的销售图，在某个时间段内，指定商品的销售数量图
 * @author zgw
 *2009-8-25 上午06:49:26
 * javazgw@gmail.com
 */
public class OneProSaleAnalysis extends ContentPanel{
	 BaseStore<JDBCModelData> store;
	 String b2cgcode;
	 String b2cgname;
	 SelectionListener<ButtonEvent> listener ;
	 int showmonth = 12; // 显示月份数
	 String datestrstart = "";
	 String datestrend = "";
	 int monthnow =0;
	 ToolBar toolbar;
	 Html html;//标题
	 
	 public OneProSaleAnalysis(String b2cgcode,String b2cgname)
	 {
		 this.b2cgcode = b2cgcode;
		 this.b2cgname = b2cgname;
		 init();
		 
	 }
	public OneProSaleAnalysis(String b2cgcode)
	{
		this.b2cgcode = b2cgcode;
		
		init();
	}
	void init()
	{
		setWidth("400px");
		this.setHeaderVisible(false);
		initlistener();
		inittoolbar();
		initdate();
		
		
		this.setTopComponent(toolbar);
		initstore();
		initWidget();
		
	}
	void initdate()
	{
		Date d = new Date();
//		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
		//DateTimeFormat dd = new DateTimeFormat();
//		Calendar cal = Calendar.getInstance();
		monthnow = d.getMonth()+1;
		int up = monthnow-(showmonth);
		d.setMonth(up);
		d.setDate(0);
		datestrstart = DateTimeFormat.getFormat("yyyy-MM-dd").format(d);
		Date d2 = new Date();
		int donw = monthnow;
		d2.setMonth(donw);
		d2.setDate(0);
		datestrend = DateTimeFormat.getFormat("yyyy-MM-dd").format(d2);
		html.setHtml(""+datestrstart+"--"+datestrend);	
	
	}
	void up()
	{
		Date d = new Date();
	//	monthnow = d.getMonth();
		monthnow -=1;
		int up = monthnow-(showmonth);
		d.setMonth(up);
		d.setDate(0);
		datestrstart = DateTimeFormat.getFormat("yyyy-MM-dd").format(d);
		Date d2 = new Date();
		int donw = monthnow;
		d2.setMonth(donw);
		d2.setDate(0);
	
		datestrend = DateTimeFormat.getFormat("yyyy-MM-dd").format(d2);
		
	
		html.setHtml(""+datestrstart+"--"+datestrend);
//		toolbar.add(html);
//		toolbar.layout();
	}
	void down()
	{
		Date d = new Date();
		monthnow +=1;
		int up = monthnow-(showmonth);
		d.setMonth(up);
		d.setDate(0);
		datestrstart = DateTimeFormat.getFormat("yyyy-MM-dd").format(d);
		Date d2 = new Date();
		int donw = monthnow;
		d2.setMonth(donw);
		d2.setDate(0);
		datestrend = DateTimeFormat.getFormat("yyyy-MM-dd").format(d2);
		html.setHtml(""+datestrstart+"--"+datestrend);
	}
	void initstore()
	{
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

	//	type.addField("b2cgcode");
		type.addField("b2cgname");
	//	type.addField("orderdate");
		type.addField("sumqty");
		type.addField("ym");
	//	type.addField("totle");

		StoreInfo info = new StoreInfo();

		info.setTablename("b2clistorderg");
		//info.setKeyname(new String[] { "b2cgcode" });
		
		System.out.println(initreloadparam());
		info.setSql(initreloadparam());
		store = new BaseStore<JDBCModelData>(type, info);

	}
	String initreloadparam()
	{
		String sqlstr = "";


		sqlstr += "select b2cgname,sumqty ,cast(yearderdate as varchar(4)) %2B '-' %2B cast(dmonth as varchar(4)) as ym from ";
		sqlstr += "(  ";
		sqlstr += "select (select b2cgname from b2cgcode where b2cgcode.b2cgcode = b2clistorderg.b2cgcode) as b2cgname,sum(cast(qty as int)) as sumqty,year(orderdate) as yearderdate ,month(orderdate) as dmonth from b2clistorderg ";

		sqlstr += "where cast(orderdate as datetime)  > '"+datestrstart+"' and  cast(orderdate as datetime)  < '"+datestrend+"'  and  b2cgcode='"+b2cgcode+"'";
		sqlstr += "group by b2cgcode,year(orderdate),month(orderdate)  ";

		sqlstr += "		) as a ";

		sqlstr += "order by ym ";
		return sqlstr;
		
	}
	void initWidget()
	{
		 ChartModel model = new ChartModel((b2cgname==null?b2cgcode:b2cgname)+":销售量趋势图",  
	        "font-size: 16px; font-family: Verdana; text-align: center;");  
		  model.setBackgroundColour("#fefefe");  
		    model.setLegend(new Legend(Position.TOP, true));  
		    model.setScaleProvider(ScaleProvider.ROUNDED_NEAREST_SCALE_PROVIDER);  
		    
		    BarChart bar = new BarChart(BarStyle.GLASS);  
		    bar.setColour("#00aaff");  
		    bar.setTooltip("#val#");
		    BarDataProvider barProvider = new BarDataProvider( "sumqty","ym");  
		    barProvider.bind(store);  
		    bar.setDataProvider(barProvider);  
		 //  bar.addChartListener(listener);  
		    model.addChartConfig(bar);  
		   
		    
		    String url = "chart/open-flash-chart.swf";
		    final Chart chart = new Chart(url);  
		   // chart.setToolTip("#label# $#val#<br>#percent#");
		    chart.setChartModel(model);  
		    chart.setHeight("300px");
		    add(chart);
	}
	
	 void initlistener()
	 {
		 listener = new SelectionListener<ButtonEvent>()
		 {

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				if(ce.getButton().getText().equals("向前"))
				{
					System.out.println("向前");
					up();
					
//					initstore();
					StoreInfo info  = store.getStoreinfo();;
					info.setSql(initreloadparam());
					store.setStoreinfo(info);
					store.reload();
				}
				else if(ce.getButton().getText().equals("向后"))
				{
					System.out.println("向后");
					down();
					StoreInfo info  = store.getStoreinfo();;
					info.setSql(initreloadparam());
					store.setStoreinfo(info);
					store.reload();
				}
				else if(ce.getButton().getText().equals("最近日期"))
				{
					System.out.println("最近日期");
					initdate();
					StoreInfo info  = store.getStoreinfo();;
					info.setSql(initreloadparam());
					store.setStoreinfo(info);
					store.reload();
				}
			}
			 
		 };
	 }
	 void inittoolbar()
	 {
		  toolbar = new ToolBar();
		 Button up = new Button("向前");
		 Button down = new Button("向后");
		 Button now = new Button("最近日期");
		// up.addSelectionListener(SelectionListener<ButtonEvent>)
		 toolbar.add(up);
		 toolbar.add(now);
		 toolbar.add(down);
		 html = new Html("");
			toolbar.add(html);
			
		 up.addSelectionListener(listener);
		 now.addSelectionListener(listener);
		 down.addSelectionListener(listener);
		 
		 
	 }
}
